package com.yanqu.road.server.manager.activity.suitang.manager;

import com.yanqu.road.entity.activity.suitang.config.SuiTangCityTypeConfig;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUnionData;
import com.yanqu.road.entity.activity.suitang.data.CrossSuiTangUserData;
import com.yanqu.road.entity.activity.suitang.entity.group.CrossSuiTangGroup;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionRound;
import com.yanqu.road.entity.activity.suitang.entity.user.CrossSuiTangUserRound;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangCityType;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangRedDotType;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangWarType;
import com.yanqu.road.server.manager.activity.suitang.activity.CrossSuiTangActivity;
import com.yanqu.road.utils.property.Property;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

public class CrossSuiTangRoundActivityMgr extends CrossBaseActivityMgr {

    protected static Logger logger = LogManager.getLogger(CrossSuiTangRoundActivityMgr.class.getName());

    @Override
    public void init() {
        initRoundData();
        logger.debug("【隋唐】轮次数据初始化完成");
    }

    /*
     * 轮次切换
     */
    public void OnRoundChange() {
        // 重置玩家轮次数据
        ResetUserRoundData();

        // 重置商会轮次数据
        ResetUnionRoundData();

        // 处理晋级奖励
        handlePromotionReward();

        activity.getSyncActivityMgr().syncBaseDataMsgToAll();
    }

    private void handlePromotionReward() {
        Map<Long, CrossSuiTangUserData> userDataMap = activity.getUserDataMap();
        for (CrossSuiTangUserData userData : userDataMap.values()) {
            CrossSuiTangUserRound curUserRound = getCurUserRound(userData.getUserId());
            if (curUserRound == null) {
                continue;
            }
            Property promotRewards = activity.getConfig().getPromotRewards(curUserRound.getRound(), curUserRound.getWarType());
            if (promotRewards == null || promotRewards.isNothing()) {
                continue;
            }
            // 加奖励
            userData.addWaitReward(promotRewards);
        }
    }

    /*
     * 初始化轮次数据
     */
    private void initRoundData() {
        Map<Long, CrossSuiTangUserData> userDataMap = activity.getUserDataMap();
        if (userDataMap == null || userDataMap.isEmpty()) {
            return;
        }
        for (Map.Entry<Long, CrossSuiTangUserData> entry : userDataMap.entrySet()) {
            initUserRoundData(entry.getKey());
        }

        Map<String, CrossSuiTangUnionData> unionDataMap = activity.getUnionDataMap();
        if (unionDataMap == null || unionDataMap.isEmpty()) {
            return;
        }
        for (CrossSuiTangUnionData unionData : unionDataMap.values()) {
            initUnionRoundData(unionData.getUnionUid());
        }

        activity.getSyncActivityMgr().syncBaseDataMsgToAll();
    }

    /*
     * 初始化玩家轮次数据
     */
    public CrossSuiTangUserRound initUserRoundData(long userId) {
        CrossSuiTangUserRound userRound = new CrossSuiTangUserRound();
        CrossSuiTangUserData userData = activity.getUserData(userId);
        if (userData == null) {
            return null;
        }
        Map<Integer, Map<eSuiTangWarType, Map<Integer, CrossSuiTangGroup>>> actGroupSuiTang = activity.getCrossSuiTangData().getActGroupMap();
        if (actGroupSuiTang == null || actGroupSuiTang.isEmpty() || actGroupSuiTang.get(calcRound()) == null) {
            return null;
        }

        // 获取角色商会的分组信息
        for (Map.Entry<eSuiTangWarType, Map<Integer, CrossSuiTangGroup>> entry : actGroupSuiTang.get(calcRound()).entrySet()) {
            eSuiTangWarType warType = entry.getKey();
            Map<Integer, CrossSuiTangGroup> groupMap = entry.getValue();
            for (Map.Entry<Integer, CrossSuiTangGroup> entryGroup : groupMap.entrySet()) {
                int actGroupId = entryGroup.getKey();
                Map<String, Integer> unionMap = entryGroup.getValue().getUnionMap();
                if (unionMap == null) {
                    continue;
                }
                Integer cityId = unionMap.getOrDefault(userData.getUnionUid(), 0);
                if (cityId == 0) {
                    continue;
                }
                userRound.setActGroupId(actGroupId);
                userRound.setWarType(warType);
                userRound.setCityId(cityId);
                userRound.setRound(calcRound());
                userRound.setPosAddJunZiMap(new ConcurrentHashMap<>());
                userRound.setPosAddJunZiTime(0);
                userRound.setContinuousKill(0);
                userRound.setUnreadCityReport(false);
                userRound.setUnreadPlayerReport(false);
                userRound.setScore(new AtomicLong(0));
                userRound.setUserId(userId);
                if (!userData.getUserRoundMap().containsKey(calcRound())) {
                    userData.getUserRoundMap().put(calcRound(), userRound);
                    userData.setUpdateOption();
                }
                break;
            }
        }
        return userRound;
    }

    /*
     * 初始化商会轮次数据
     */
    public CrossSuiTangUnionRound initUnionRoundData(String unionId) {
        CrossSuiTangUnionRound unionRound = new CrossSuiTangUnionRound();
        Map<String, CrossSuiTangUnionData> unionDataMap = activity.getUnionDataMap();
        if (unionDataMap == null || unionDataMap.isEmpty()) {
            return null;
        }
        CrossSuiTangUnionData unionData = unionDataMap.get(unionId);
        if (unionData == null) {
            return null;
        }
        Map<Integer, Map<eSuiTangWarType, Map<Integer, CrossSuiTangGroup>>> actGroupSuiTang = activity.getCrossSuiTangData().getActGroupMap();
        if (actGroupSuiTang == null || actGroupSuiTang.isEmpty() || actGroupSuiTang.get(calcRound()) == null) {
            return null;
        }

        for (Map.Entry<eSuiTangWarType, Map<Integer, CrossSuiTangGroup>> entry : actGroupSuiTang.get(calcRound()).entrySet()) {
            eSuiTangWarType warType = entry.getKey();
            Map<Integer, CrossSuiTangGroup> groupMap = entry.getValue();
            for (Map.Entry<Integer, CrossSuiTangGroup> entryGroup : groupMap.entrySet()) {
                int actGroupId = entryGroup.getKey();
                Map<String, Integer> unionMap = entryGroup.getValue().getUnionMap();
                if (unionMap == null) {
                    continue;
                }
                Integer cityId = unionMap.getOrDefault(unionId, 0);
                if (cityId == 0) {
                    continue;
                }
                SuiTangCityTypeConfig suiTangCityTypeConfig = activity.getConfig().getCityTypeConfigMap().get(activity.getConfig().getCityTypeConfigKey(warType, eSuiTangCityType.CITY_TYPE_HOME.getType()));
                long score = suiTangCityTypeConfig == null ? 0 : suiTangCityTypeConfig.getCampPoint(calcRound() - 1);
                unionRound.setActGroupId(actGroupId);
                unionRound.setWarType(warType);
                unionRound.setHomeId(cityId);
                unionRound.setRound(calcRound());
                unionRound.setCurrentCityScore(new AtomicLong(score));
                unionRound.setUnionUid(unionId);
                unionRound.setUnionTaskMap(new ConcurrentHashMap<>());
                unionData.getUnionRoundMap().put(calcRound(), unionRound);
                unionData.setUpdateOption();
                // 加入战场内分组榜单
//                activity.getRankActivityMgr().updateWarUnionRank(unionRound.getUnionUid(), 0, System.currentTimeMillis());
                break;
            }
        }
        return null;
    }

    /*
     * 重置玩家轮次数据
     */
    private void ResetUserRoundData() {
        Map<Long, CrossSuiTangUserData> userDataMap = activity.getUserDataMap();
        if (userDataMap == null || userDataMap.isEmpty()) {
            return;
        }
        for (Map.Entry<Long, CrossSuiTangUserData> entry : userDataMap.entrySet()) {
            CrossSuiTangUserData userData = entry.getValue();
            if (userData == null) {
                continue;
            }
            initUserRoundData(userData.getUserId());
            // 红点也要重置一下
            readRedDotByCmd(userData.getUserId(), eSuiTangRedDotType.Type3_CityWarReport.getValue());
            readRedDotByCmd(userData.getUserId(), eSuiTangRedDotType.Type2_UserWarReport.getValue());
        }
    }

    /*
     * 重置商会轮次数据
     */
    private void ResetUnionRoundData() {
        // 重置数据
        Map<String, CrossSuiTangUnionData> unionDataMap = activity.getUnionDataMap();
        if (unionDataMap == null || unionDataMap.isEmpty()) {
            return;
        }
        for (CrossSuiTangUnionData unionData : unionDataMap.values()) {
            initUnionRoundData(unionData.getUnionUid());
        }
    }

    /*
     * 补发上一轮未领取的军资
     */
    private void JunZiAutoSend(Map<Long, Map<Long, BigInteger>> posAddJunZiMap) {

    }

    /*
     * 计算轮次
     */
    public int calcRound() {
        if (activity == null) {
            return 0;
        }
        return activity.calcRound();
    }

    /*
     * setActivity
     */
    public void setActivity(CrossSuiTangActivity activity) {
        this.activity = activity;
    }
}
